/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.assg2.dao;

import com.movie.Cinema;
import com.movie.Movie;
import com.movie.Show;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.*;

/**
 *
 * @author Kevin
 */
public class DaoShow extends GenericDao {

    public DaoShow() {
        super(Show.class);
    }

    public List<Show> findByMovieAndCinema(Movie m, Cinema c) {
        Query q = query("select distinct s from Cinema c, Movie m, Show s, Room r where c.idcinema=r.cinemaIdcinema and r.idroom=s.roomIdroom and s.movieIdmovie=m.idmovie and m.idmovie=" + Integer.toString(m.getIdmovie()) + " and c.location='" + c.getLocation() + "'");
        if (q.list().get(0) != null) {
            return q.list();
        } else {
            return null;
        }
    }

    public int SeatsAvailable(Show s, Date date) {
        Query q = query("select sum(b.numberofseats) from Book b, Show s where b.showIdshow=s.idshow and s.idshow=" + Integer.toString(s.getIdshow()) + " and b.date=:showDate").setParameter("showDate", date);
        if (q.list().get(0) != null) {
            return s.getRoom().getNumberofseats() - Integer.parseInt(q.list().get(0).toString());
        } else {
            return s.getRoom().getNumberofseats();
        }
    }

    public int SeatsAvailablePrint(Show s, Date date) {
        Query q = query("select sum(b.numberofseats) from Book b, Show s where b.showIdshow=s.idshow and s.idshow=" + Integer.toString(s.getIdshow()) + " and b.date=:showDate").setParameter("showDate", date);
        System.out.print(q.toString());
        if (q.list().get(0) != null) {
            return s.getRoom().getNumberofseats() - Integer.parseInt(q.list().get(0).toString());
        } else {
            return s.getRoom().getNumberofseats();
        }
    }
}
